﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using System.ComponentModel.Composition;

namespace NeuroSpeech.XamlExtensions
{
    [Export]
    public class RefConverter : GenericConverter<Ref>
    {

        public override CodeStatement Assign(XamlCodeContext context, CodeExpression target, CodeExpression value)
        {

            // remove previous...

            CodeVariableDeclarationStatement s = context.GetLastStatement<CodeVariableDeclarationStatement>(context.PreStatements);
            CodeObjectCreateExpression cc = s.InitExpression as CodeObjectCreateExpression;
            CodePrimitiveExpression exp = cc.Parameters[0] as CodePrimitiveExpression;
            string textValue = exp.Value as string;
            context.PreStatements.Remove(s);
            return new CodeAssignStatement {
                Left = target,
                Right = new CodeSnippetExpression(textValue)
            };
        }

    }
}
